package com.yaoxun.bored.common;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.common.Mapper;

@Transactional
public abstract class BaseService<T> {
	
	@Inject
	private Mapper<T> mapper;
	
	@Inject
	private MyMapper<T> myMapper;
	
	public void insert(T entity) {
		mapper.insert(entity);
	}
	
	public T selectByPrimaryKey(Object key) {
		return mapper.selectByPrimaryKey(key);
	}
	
	public List<T> selectByExample(Object example) {
		return mapper.selectByExample(example);		
	}
	
	public int selectCountByExample(Object example) {
		return mapper.selectCountByExample(example);
	}
	
	public T selectOne(Serializable id) {
		return mapper.selectByPrimaryKey(id);
	}
	
	public int updateByPrimaryKeySelective(T entity) {
		return mapper.updateByPrimaryKeySelective(entity);
	}
	
	public int deleteByPrimaryKey(Object key) {
		return mapper.deleteByPrimaryKey(key);
	}
	
	public int deleteByExample(Object example) {
		return mapper.deleteByExample(example);
	}
	
	@Transactional(readOnly=true)
	public PageInfo<Map<String, Object>> selectByPaging(Object query) {
		QueryParams param = (QueryParams) query;
		PageHelper.startPage(param.getPageInt(), param.getLimitInt(), param.getOrderBy());
		List<Map<String,Object>> list = myMapper.selectByPaging(query);
		PageInfo<Map<String, Object>> info = new PageInfo<>(list);
		return info;
	}
	
}
